import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map.Entry;

import org.jfree.data.xy.XYSeries;


import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.lazy.IB1;
import weka.classifiers.trees.J48;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;


/**
 * 
 * play with ARFF File (http://weka.wikispaces.com/ARFF+%28book+version%29)
 * see trainingSet.arff
 * 
 * http://weka.wikispaces.com/Use+Weka+in+your+Java+code
 * 
 * @author paul
 *
 */
public class PlayWithWeka {

    /**
     * Only tests, not meant to be run for production
     * 
     */
	public static void main(String args[]) {	
		 try {
			Instances trainingSet = getDataSetFromFile("trainingSetNew.arff");

			// Should reate a plurality voting classifier (see paper from Ravi et al) 
			// Classifier algo = new NaivesBayes(); // 10-fold cross vald. : Correctly classifier : 80%
			// Classifier algo = new J48(); // 10-fold cross vald. : Correctly classifier : 86%
			Classifier algo = new NaiveBayes(); // Eval : 95%!!
			ClassifierBuilder myClassifier = 
					(new ClassifierBuilder(algo)).build(trainingSet);
			Evaluation eval = myClassifier.evaluate();

			System.out.println(eval.toSummaryString());
			System.out.println(eval.toMatrixString());
			
			// Specify that the instance belong to the training set 
			// in order to inherit from the set description
			DataWindow dw = StructuredData.getAllDataFrom(ToolBox.pathnames[9],myClassifier).get(0);
			
    		Instance iUse = dw.featuresInstanceExtraction();	
    		
    		dw.plotSignal();    		
    		System.out.println(dw.countSteps());
			
			// Get the likelihood of each classes
			for (double d: myClassifier.getLikelihood(iUse)) {
				System.out.println(d);				
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Instances getDataSetFromFile(String arfffilename) throws Exception {
    	ArffLoader loader = new ArffLoader();
		loader.setFile(new File(arfffilename));
	    Instances data = loader.getDataSet();
		 // setting class attribute if the data format does not provide this information
		 // For example, the XRFF format saves the class attribute information as well
		 if (data.classIndex() == -1)
		   data.setClassIndex(data.numAttributes() - 1);
		 
		 return data;
	}
}

